<?php

class ControllerSaleSpeedy extends Controller {

    private $error = array();
    private $speedy;

    public function __construct($registry) {
        parent::__construct($registry);
        require_once(DIR_SYSTEM . 'library/speedy.php');
        $this->speedy = new Speedy($registry);
    }

    public function index() {
        $this->language->load('sale/speedy');

        $this->session->data['is_speedy_bol_recalculated'] = 0;
        $this->session->data['is_speedy_bol_recalculated_orderid'] = 0;

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('sale/speedy');

        $this->getList();
    }

    public function create() {
        $this->language->load('sale/speedy');

        $this->document->setTitle($this->language->get('heading_title_details'));

        $this->load->model('sale/speedy');

        $url = '';

        if (isset($this->request->get['filter_bol_id'])) {
            $url .= '&filter_bol_id=' . $this->request->get['filter_bol_id'];
        }

        if (isset($this->request->get['filter_order_id'])) {
            $url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
        }

        if (isset($this->request->get['filter_customer'])) {
            $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_date_created'])) {
            $url .= '&filter_date_created=' . $this->request->get['filter_date_created'];
        }

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        if (isset($this->request->get['order_id'])) {
            $speedy_order_info = $this->model_sale_speedy->getOrder($this->request->get['order_id']);
        }

        if (!empty($speedy_order_info)) {
            $speedy_order_data = unserialize($speedy_order_info['data']);

            if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm() && !$this->request->post['calculate']) {
                $this->request->post['taking_date'] = strtotime($this->request->post['taking_date']);

                if (isset($this->request->post['shipping_method'])) {
                    $shipping_method_info = explode('.', $this->request->post['shipping_method']);

                    $this->request->post['shipping_method_id'] = $shipping_method_info[1];
                    $this->request->post['shipping_method_cost'] = $this->session->data['shipping_method_cost'][$shipping_method_info[1]];
                    $this->request->post['shipping_method_title'] = $this->session->data['shipping_method_title'][$shipping_method_info[1]];

                    
                }

                if (isset($this->request->post['fixed_time_cb'])) {
                    $this->request->post['fixed_time'] = $this->request->post['fixed_time_hour'] . $this->request->post['fixed_time_min'];
                } else {
                    $this->request->post['fixed_time'] = null;
                }

                $this->load->model('sale/order');
                $order_info = $this->model_sale_order->getOrder($this->request->get['order_id']);

                unset($speedy_order_data['fixed_time_cb']);
                $data = array_merge($speedy_order_data, $this->request->post);

                $bol = $this->speedy->createBillOfLading($data, $order_info);
                if (isset($this->request->post['fixed_time_cb']) && isset($this->session->data['loading_methods_fixed_time'][$shipping_method_info[1]])) {
                        $this->request->post['shipping_method_cost'] += $this->session->data['loading_methods_fixed_time'][$shipping_method_info[1]];
                    }

                if (!$this->speedy->getError() && $bol) {
                    $this->model_sale_speedy->editOrder($this->request->get['order_id'], array('bol_id' => $bol['bol_id']));

                    if ($this->session->data['is_speedy_bol_recalculated']) {
                        
                        if (($this->config->get('speedy_pricing') == 'free') && ($data['total'] >= (float) $this->config->get('speedy_free_shipping_total')) &&
                                ($data['shipping_method_id'] == $this->config->get('speedy_free_method_city') || $data['shipping_method_id'] == $this->config->get('speedy_free_method_intercity'))) {
                            $data['shipping_method_cost'] = 0;
                        } elseif ($this->config->get('speedy_pricing') == 'fixed') {


                            $data['shipping_method_cost'] = $this->config->get('speedy_fixed_price');
                        } else {
                            $data['shipping_method_cost'] = $bol['total'];

                            if ($this->config->get('speedy_pricing') == 'calculator_fixed') {
                                $data['shipping_method_cost'] += $this->config->get('speedy_fixed_price');
                            }
                        }
                    }

                    $comment = $this->model_sale_speedy->updateOrderInfo($this->request->get['order_id'], $data);

                    $history_data = array(
                        'order_status_id' => $this->config->get('speedy_order_status_id'),
                        'notify' => true,
                        'comment' => $comment
                    );



                    $this->model_sale_order->addOrderHistory($this->request->get['order_id'], $history_data);

                    $this->session->data['success'] = sprintf($this->language->get('text_success_create'), '<a href="' . $this->url->link('sale/speedy/printPDF', 'token=' . $this->session->data['token'] . '&order_id=' . $this->request->get['order_id'] . $url, 'SSL') . '" target="_blank">' . $bol['bol_id'] . '</a>');
                    $this->session->data['is_speedy_bol_recalculated'] = 0;
                    $this->session->data['is_speedy_bol_recalculated_orderid'] = 0;
                    $this->redirect($this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . $url, 'SSL'));
                } else {
                    if (stripos($this->speedy->getError(), 'Not valid serviceTypeId') !== false) {
                        $this->error['warning'] = $this->language->get('error_calculate');
                    } else {
                        $this->error['warning'] = $this->speedy->getError();
                    }
                }
            }

            $this->data['heading_title'] = $this->language->get('heading_title_details');

            $this->data['text_calculate'] = $this->language->get('text_calculate');
            $this->data['text_wait'] = $this->language->get('text_wait');
            $this->data['text_yes'] = $this->language->get('text_yes');
            $this->data['text_no'] = $this->language->get('text_no');
            $this->data['text_to_office'] = $this->language->get('text_to_office');
            $this->data['text_to_door'] = $this->language->get('text_to_door');
            $this->data['text_select_city'] = $this->language->get('text_select_city');
            $this->data['text_fixed_time'] = $this->language->get('text_fixed_time');

            $this->data['entry_contents'] = $this->language->get('entry_contents');
            $this->data['entry_weight'] = $this->language->get('entry_weight');
            $this->data['entry_packing'] = $this->language->get('entry_packing');
            $this->data['entry_count'] = $this->language->get('entry_count');
            $this->data['entry_size'] = $this->language->get('entry_size');
            $this->data['entry_shipping_method'] = $this->language->get('entry_shipping_method');
            $this->data['entry_deffered_days'] = $this->language->get('entry_deffered_days');
            $this->data['entry_client_note'] = $this->language->get('entry_client_note');
            $this->data['entry_cod'] = $this->language->get('entry_cod');
            $this->data['entry_total'] = $this->language->get('entry_total');
            $this->data['entry_insurance'] = $this->language->get('entry_insurance');
            $this->data['entry_fragile'] = $this->language->get('entry_fragile');
            $this->data['entry_total_insurance'] = $this->language->get('entry_total_insurance');
            $this->data['entry_shipping_to'] = $this->language->get('entry_shipping_to');
            $this->data['entry_postcode'] = $this->language->get('entry_postcode');
            $this->data['entry_city'] = $this->language->get('entry_city');
            $this->data['entry_quarter'] = $this->language->get('entry_quarter');
            $this->data['entry_street'] = $this->language->get('entry_street');
            $this->data['entry_street_no'] = $this->language->get('entry_street_no');
            $this->data['entry_block_no'] = $this->language->get('entry_block_no');
            $this->data['entry_entrance_no'] = $this->language->get('entry_entrance_no');
            $this->data['entry_floor_no'] = $this->language->get('entry_floor_no');
            $this->data['entry_apartment_no'] = $this->language->get('entry_apartment_no');
            $this->data['entry_office'] = $this->language->get('entry_office');
            $this->data['entry_note'] = $this->language->get('entry_note');
            $this->data['entry_fixed_time'] = $this->language->get('entry_fixed_time');

            $this->data['button_create'] = $this->language->get('button_create');
            $this->data['button_cancel'] = $this->language->get('button_cancel');
            $this->data['button_calculate'] = $this->language->get('button_calculate');

            if (isset($this->error['warning'])) {
                $this->data['error_warning'] = $this->error['warning'];
            } else {
                $this->data['error_warning'] = '';
            }

            if (isset($this->error['contents'])) {
                $this->data['error_contents'] = $this->error['contents'];
            } else {
                $this->data['error_contents'] = '';
            }

            if (isset($this->error['weight'])) {
                $this->data['error_weight'] = $this->error['weight'];
            } else {
                $this->data['error_weight'] = '';
            }

            if (isset($this->error['packing'])) {
                $this->data['error_packing'] = $this->error['packing'];
            } else {
                $this->data['error_packing'] = '';
            }

            if (isset($this->error['count'])) {
                $this->data['error_count'] = $this->error['count'];
            } else {
                $this->data['error_count'] = '';
            }

            if (isset($this->error['address'])) {
                $this->data['error_address'] = $this->error['address'];
            } else {
                $this->data['error_address'] = '';
            }

            if (isset($this->error['office'])) {
                $this->data['error_office'] = $this->error['office'];
            } else {
                $this->data['error_office'] = '';
            }

            if (isset($this->error['fixed_time'])) {
                $this->data['error_fixed_time'] = $this->error['fixed_time'];
            } else {
                $this->data['error_fixed_time'] = '';
            }

            $this->data['breadcrumbs'] = array();

            $this->data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
                'separator' => false
            );

            $this->data['breadcrumbs'][] = array(
                'text' => $this->language->get('heading_title_details'),
                'href' => $this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . $url, 'SSL'),
                'separator' => ' :: '
            );

            $this->data['action'] = $this->url->link('sale/speedy/create', 'token=' . $this->session->data['token'] . '&order_id=' . $this->request->get['order_id'] . $url, 'SSL');
            $this->data['cancel'] = $this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . $url, 'SSL');

            $this->data['token'] = $this->session->data['token'];

            if (isset($this->request->post['contents'])) {
                $this->data['contents'] = $this->request->post['contents'];
            } elseif (isset($speedy_order_data['contents'])) {
                $this->data['contents'] = $speedy_order_data['contents'];
            } else {
                $this->data['contents'] = $this->language->get('text_contents') . ' ' . $this->request->get['order_id'];
            }

            if (isset($this->request->post['weight'])) {
                $this->data['weight'] = $this->request->post['weight'];
            } elseif (isset($speedy_order_data['weight'])) {
                $this->data['weight'] = $speedy_order_data['weight'];
            } else {
                $this->data['weight'] = '';
            }

            if (isset($this->request->post['packing'])) {
                $this->data['packing'] = $this->request->post['packing'];
            } elseif (isset($speedy_order_data['packing'])) {
                $this->data['packing'] = $speedy_order_data['packing'];
            } else {
                $this->data['packing'] = $this->config->get('speedy_packing');
            }

            if (isset($this->request->post['count'])) {
                $this->data['count'] = $this->request->post['count'];
            } elseif (isset($speedy_order_data['count'])) {
                $this->data['count'] = $speedy_order_data['count'];
            } else {
                $this->data['count'] = 1;
            }

            if (isset($this->request->post['width'])) {
                $this->data['width'] = $this->request->post['width'];
            } elseif (isset($speedy_order_data['width'])) {
                $this->data['width'] = $speedy_order_data['width'];
            } else {
                $this->data['width'] = '';
            }

            if (isset($this->request->post['height'])) {
                $this->data['height'] = $this->request->post['height'];
            } elseif (isset($speedy_order_data['height'])) {
                $this->data['height'] = $speedy_order_data['height'];
            } else {
                $this->data['height'] = '';
            }

            if (isset($this->request->post['depth'])) {
                $this->data['depth'] = $this->request->post['depth'];
            } elseif (isset($speedy_order_data['depth'])) {
                $this->data['depth'] = $speedy_order_data['depth'];
            } else {
                $this->data['depth'] = '';
            }

            if (isset($this->request->post['shipping_method'])) {
                $shipping_method = explode('.', $this->request->post['shipping_method']);
                $this->data['shipping_method_id'] = $shipping_method[1];
            } elseif (isset($speedy_order_data['shipping_method_id'])) {
                $this->data['shipping_method_id'] = $speedy_order_data['shipping_method_id'];
            } else {
                $this->data['shipping_method_id'] = '';
            }

            if (isset($this->request->post['deffered_days'])) {
                $this->data['deffered_days'] = $this->request->post['deffered_days'];
            } elseif (isset($speedy_order_data['deffered_days'])) {
                $this->data['deffered_days'] = $speedy_order_data['deffered_days'];
            } else {
                $this->data['deffered_days'] = 0;
            }

            if (isset($this->request->post['client_note'])) {
                $this->data['client_note'] = $this->request->post['client_note'];
            } elseif (isset($speedy_order_data['client_note'])) {
                $this->data['client_note'] = $speedy_order_data['client_note'];
            } else {
                $this->data['client_note'] = '';
            }

            if (isset($this->request->post['cod'])) {
                $this->data['cod'] = $this->request->post['cod'];
            } elseif (isset($speedy_order_data['cod'])) {
                $this->data['cod'] = $speedy_order_data['cod'];
            } else {
                $this->data['cod'] = true;
            }

            if (isset($this->request->post['total'])) {
                $this->data['total'] = $this->request->post['total'];
            } elseif (isset($speedy_order_data['total'])) {
                $this->data['total'] = $speedy_order_data['total'];
            } else {
                $this->data['total'] = '';
            }

            if (isset($this->request->post['insurance'])) {
                $this->data['insurance'] = $this->request->post['insurance'];
            } elseif (isset($speedy_order_data['insurance'])) {
                $this->data['insurance'] = $speedy_order_data['insurance'];
            } else {
                $this->data['insurance'] = $this->config->get('speedy_insurance');
            }

            if (isset($this->request->post['fragile'])) {
                $this->data['fragile'] = $this->request->post['fragile'];
            } elseif (isset($speedy_order_data['fragile'])) {
                $this->data['fragile'] = $speedy_order_data['fragile'];
            } else {
                $this->data['fragile'] = $this->config->get('speedy_fragile');
            }

            if (isset($this->request->post['totalNoShipping'])) {
                $this->data['totalNoShipping'] = $this->request->post['totalNoShipping'];
            } elseif (isset($speedy_order_data['totalNoShipping'])) {
                $this->data['totalNoShipping'] = $speedy_order_data['totalNoShipping'];
            } else {
                $this->data['totalNoShipping'] = '';
            }

            if (isset($this->request->post['to_office'])) {
                $this->data['to_office'] = $this->request->post['to_office'];
            } elseif (isset($speedy_order_data['to_office'])) {
                $this->data['to_office'] = $speedy_order_data['to_office'];
            } else {
                $this->data['to_office'] = 0;
            }

            if (isset($this->request->post['postcode'])) {
                $this->data['postcode'] = $this->request->post['postcode'];
            } elseif (isset($speedy_order_data['postcode'])) {
                $this->data['postcode'] = $speedy_order_data['postcode'];
            } else {
                $this->data['postcode'] = '';
            }

            if (isset($this->request->post['city'])) {
                $this->data['city'] = $this->request->post['city'];
            } elseif (isset($speedy_order_data['city'])) {
                $this->data['city'] = $speedy_order_data['city'];
            } else {
                $this->data['city'] = '';
            }

            if (isset($this->request->post['city_id'])) {
                $this->data['city_id'] = $this->request->post['city_id'];
            } elseif (isset($speedy_order_data['city_id'])) {
                $this->data['city_id'] = $speedy_order_data['city_id'];
            } else {
                $this->data['city_id'] = 0;
            }

            if (isset($this->request->post['city_nomenclature'])) {
                $this->data['city_nomenclature'] = $this->request->post['city_nomenclature'];
            } elseif (isset($speedy_order_data['city_nomenclature'])) {
                $this->data['city_nomenclature'] = $speedy_order_data['city_nomenclature'];
            } else {
                $this->data['city_nomenclature'] = '';
            }

            if (isset($this->request->post['quarter'])) {
                $this->data['quarter'] = $this->request->post['quarter'];
            } elseif (isset($speedy_order_data['quarter'])) {
                $this->data['quarter'] = $speedy_order_data['quarter'];
            } else {
                $this->data['quarter'] = '';
            }

            if (isset($this->request->post['quarter_id'])) {
                $this->data['quarter_id'] = $this->request->post['quarter_id'];
            } elseif (isset($speedy_order_data['quarter_id'])) {
                $this->data['quarter_id'] = $speedy_order_data['quarter_id'];
            } else {
                $this->data['quarter_id'] = 0;
            }

            if (isset($this->request->post['street'])) {
                $this->data['street'] = $this->request->post['street'];
            } elseif (isset($speedy_order_data['street'])) {
                $this->data['street'] = $speedy_order_data['street'];
            } else {
                $this->data['street'] = '';
            }

            if (isset($this->request->post['street_id'])) {
                $this->data['street_id'] = $this->request->post['street_id'];
            } elseif (isset($speedy_order_data['street_id'])) {
                $this->data['street_id'] = $speedy_order_data['street_id'];
            } else {
                $this->data['street_id'] = 0;
            }

            if (isset($this->request->post['street_no'])) {
                $this->data['street_no'] = $this->request->post['street_no'];
            } elseif (isset($speedy_order_data['street_no'])) {
                $this->data['street_no'] = $speedy_order_data['street_no'];
            } else {
                $this->data['street_no'] = '';
            }

            if (isset($this->request->post['block_no'])) {
                $this->data['block_no'] = $this->request->post['block_no'];
            } elseif (isset($speedy_order_data['block_no'])) {
                $this->data['block_no'] = $speedy_order_data['block_no'];
            } else {
                $this->data['block_no'] = '';
            }

            if (isset($this->request->post['entrance_no'])) {
                $this->data['entrance_no'] = $this->request->post['entrance_no'];
            } elseif (isset($speedy_order_data['entrance_no'])) {
                $this->data['entrance_no'] = $speedy_order_data['entrance_no'];
            } else {
                $this->data['entrance_no'] = '';
            }

            if (isset($this->request->post['floor_no'])) {
                $this->data['floor_no'] = $this->request->post['floor_no'];
            } elseif (isset($speedy_order_data['floor_no'])) {
                $this->data['floor_no'] = $speedy_order_data['floor_no'];
            } else {
                $this->data['floor_no'] = '';
            }

            if (isset($this->request->post['apartment_no'])) {
                $this->data['apartment_no'] = $this->request->post['apartment_no'];
            } elseif (isset($speedy_order_data['apartment_no'])) {
                $this->data['apartment_no'] = $speedy_order_data['apartment_no'];
            } else {
                $this->data['apartment_no'] = '';
            }

            if (isset($this->request->post['office_id'])) {
                $this->data['office_id'] = $this->request->post['office_id'];
            } elseif (isset($speedy_order_data['office_id'])) {
                $this->data['office_id'] = $speedy_order_data['office_id'];
            } else {
                $this->data['office_id'] = 0;
            }

            if (isset($this->request->post['note'])) {
                $this->data['note'] = $this->request->post['note'];
            } elseif (isset($speedy_order_data['note'])) {
                $this->data['note'] = $speedy_order_data['note'];
            } else {
                $this->data['note'] = '';
            }

            if (isset($this->request->post['fixed_time_cb'])) {
                $this->data['fixed_time_cb'] = $this->request->post['fixed_time_cb'];
            } elseif (isset($speedy_order_data['fixed_time_cb']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {
                $this->data['fixed_time_cb'] = $speedy_order_data['fixed_time_cb'];
            } else {
                $this->data['fixed_time_cb'] = false;
            }

            if (isset($this->request->post['fixed_time_hour'])) {
                $this->data['fixed_time_hour'] = $this->request->post['fixed_time_hour'];
            } elseif (isset($speedy_order_data['fixed_time_hour'])) {
                $this->data['fixed_time_hour'] = $speedy_order_data['fixed_time_hour'];
            } else {
                $this->data['fixed_time_hour'] = '';
            }

            if (isset($this->request->post['fixed_time_min'])) {
                $this->data['fixed_time_min'] = $this->request->post['fixed_time_min'];
            } elseif (isset($speedy_order_data['fixed_time_min'])) {
                $this->data['fixed_time_min'] = $speedy_order_data['fixed_time_min'];
            } else {
                $this->data['fixed_time_min'] = '';
            }

            $this->data['fixed_time'] = $this->config->get('speedy_fixed_time');

            $this->data['offices'] = array();

            if ($this->data['city_id']) {
                $this->data['offices'] = $this->speedy->getOffices(null, $this->data['city_id']);

                if ($this->speedy->getError()) {
                    $this->data['error_office'] = $this->speedy->getError();
                }
            }

            $this->data['days'] = array(0, 1, 2);
            $this->data['taking_date'] = date('d-m-Y', ($this->config->get('speedy_taking_date') ? strtotime('+' . (int) $this->config->get('speedy_taking_date') . ' day', mktime(9, 0, 0)) : time()));

            if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validateForm() && $this->request->post['calculate']) {
                $this->data['quote'] = $this->getQuote();
                $this->session->data['is_speedy_bol_recalculated'] = 1;


                if (isset($this->data['quote']['speedy_error'])) {
                    $this->data['error_warning'] = $this->data['quote']['speedy_error'];

                    $this->data['quote'] = array();
                }
            } else {
                $this->data['quote'] = array();
            }

            $this->template = 'sale/speedy_form.tpl';
            $this->children = array(
                'common/header',
                'common/footer'
            );

            $this->response->setOutput($this->render());
        } else {
            $this->language->load('error/not_found');

            $this->document->setTitle($this->language->get('heading_title'));

            $this->data['heading_title'] = $this->language->get('heading_title');

            $this->data['text_not_found'] = $this->language->get('text_not_found');

            $this->data['breadcrumbs'] = array();

            $this->data['breadcrumbs'][] = array(
                'text' => $this->language->get('text_home'),
                'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
                'separator' => false
            );

            $this->data['breadcrumbs'][] = array(
                'text' => $this->language->get('heading_title'),
                'href' => $this->url->link('error/not_found', 'token=' . $this->session->data['token'], 'SSL'),
                'separator' => ' :: '
            );

            $this->template = 'error/not_found.tpl';
            $this->children = array(
                'common/header',
                'common/footer'
            );

            $this->response->setOutput($this->render());
        }
    }

    public function printPDF() {
        $this->language->load('sale/speedy');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('sale/speedy');

        $url = '';

        if (isset($this->request->get['filter_bol_id'])) {
            $url .= '&filter_bol_id=' . $this->request->get['filter_bol_id'];
        }

        if (isset($this->request->get['filter_order_id'])) {
            $url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
        }

        if (isset($this->request->get['filter_customer'])) {
            $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_date_created'])) {
            $url .= '&filter_date_created=' . $this->request->get['filter_date_created'];
        }

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        if (isset($this->request->get['order_id'])) {
            $speedy_order_info = $this->model_sale_speedy->getOrder($this->request->get['order_id']);
        }

        if (!empty($speedy_order_info) && !empty($speedy_order_info['bol_id'])) {
            $pdf = $this->speedy->createPDF($speedy_order_info['bol_id']);

            if (!$this->speedy->getError() && $pdf) {
                //$this->response->addHeader('Content-Type: application/pdf');
                //$this->response->setOutput($pdf);
                header('Content-Type: application/pdf');
                echo $pdf;
                exit;
            } else {
                $this->session->data['warning'] = $this->speedy->getError();
            }
        } else {
            $this->session->data['warning'] = $this->language->get('error_exists');
        }

        $this->redirect($this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . $url, 'SSL'));
    }

    public function courier() {
        $this->language->load('sale/speedy');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('sale/speedy');

        $url = '';

        if (isset($this->request->get['filter_bol_id'])) {
            $url .= '&filter_bol_id=' . $this->request->get['filter_bol_id'];
        }

        if (isset($this->request->get['filter_order_id'])) {
            $url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
        }

        if (isset($this->request->get['filter_customer'])) {
            $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_date_created'])) {
            $url .= '&filter_date_created=' . $this->request->get['filter_date_created'];
        }

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $bol_ids = array();

        if (isset($this->request->get['order_id']) || isset($this->request->post['selected'])) {
            if (isset($this->request->get['order_id'])) {
                $speedy_order_info = $this->model_sale_speedy->getOrder($this->request->get['order_id']);

                if ($speedy_order_info['bol_id']) {
                    $bol_ids[] = $speedy_order_info['bol_id'];
                }
            } else {
                foreach ($this->request->post['selected'] as $order_id) {
                    $speedy_order_info = $this->model_sale_speedy->getOrder($order_id);

                    if ($speedy_order_info['bol_id']) {
                        $bol_ids[] = $speedy_order_info['bol_id'];
                    }
                }
            }
        }

        if ($bol_ids) {
            $results = $this->speedy->requestCourier($bol_ids);

            if (!$this->speedy->getError()) {
                $error = array();

                foreach ($results as $result) {
                    if (!$result->getErrorDescriptions()) {
                        $this->model_sale_speedy->editOrderCourier($result->getBillOfLading(), true);
                    } else {
                        $error[] = $result->getBillOfLading() . ' - ' . implode(', ', $result->getErrorDescriptions());
                    }
                }

                if ($error) {
                    $this->session->data['warning'] = implode('<br />', $error);
                } else {
                    $this->session->data['success'] = $this->language->get('text_success_courier');
                }
            } else {
                $this->session->data['warning'] = $this->speedy->getError();
            }
        } else {
            $this->session->data['warning'] = $this->language->get('error_exists');
        }

        $this->redirect($this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . $url, 'SSL'));
    }

    public function cancel() {
        $this->language->load('sale/speedy');

        $this->document->setTitle($this->language->get('heading_title'));

        $this->load->model('sale/speedy');

        $url = '';

        if (isset($this->request->get['filter_bol_id'])) {
            $url .= '&filter_bol_id=' . $this->request->get['filter_bol_id'];
        }

        if (isset($this->request->get['filter_order_id'])) {
            $url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
        }

        if (isset($this->request->get['filter_customer'])) {
            $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_date_created'])) {
            $url .= '&filter_date_created=' . $this->request->get['filter_date_created'];
        }

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        if (isset($this->request->get['order_id'])) {
            $speedy_order_info = $this->model_sale_speedy->getOrder($this->request->get['order_id']);
        }

        if (!empty($speedy_order_info) && !empty($speedy_order_info['bol_id']) && $this->validateDelete()) {
            $cancelled = $this->speedy->cancelBol($speedy_order_info['bol_id']);

            if (!$this->speedy->getError() && $cancelled) {
                $this->model_sale_speedy->deleteOrder($this->request->get['order_id']);

                $this->session->data['success'] = $this->language->get('text_success_cancel');
            } else {
                $this->session->data['warning'] = $this->speedy->getError();
            }
        } else {
            if (isset($this->error['warning'])) {
                $this->session->data['warning'] = $this->error['warning'];
            } else {
                $this->session->data['warning'] = $this->language->get('error_exists');
            }
        }

        $this->redirect($this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . $url, 'SSL'));
    }

    protected function getList() {
        if (isset($this->request->get['filter_bol_id'])) {
            $filter_bol_id = $this->request->get['filter_bol_id'];
        } else {
            $filter_bol_id = null;
        }

        if (isset($this->request->get['filter_order_id'])) {
            $filter_order_id = $this->request->get['filter_order_id'];
        } else {
            $filter_order_id = null;
        }

        if (isset($this->request->get['filter_customer'])) {
            $filter_customer = $this->request->get['filter_customer'];
        } else {
            $filter_customer = null;
        }

        if (isset($this->request->get['filter_date_created'])) {
            $filter_date_created = $this->request->get['filter_date_created'];
        } else {
            $filter_date_created = null;
        }

        if (isset($this->request->get['sort'])) {
            $sort = $this->request->get['sort'];
        } else {
            $sort = 'so.bol_id';
        }

        if (isset($this->request->get['order'])) {
            $order = $this->request->get['order'];
        } else {
            $order = 'DESC';
        }

        if (isset($this->request->get['page'])) {
            $page = $this->request->get['page'];
        } else {
            $page = 1;
        }

        $url = '';

        if (isset($this->request->get['filter_bol_id'])) {
            $url .= '&filter_bol_id=' . $this->request->get['filter_bol_id'];
        }

        if (isset($this->request->get['filter_order_id'])) {
            $url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
        }

        if (isset($this->request->get['filter_customer'])) {
            $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_date_created'])) {
            $url .= '&filter_date_created=' . $this->request->get['filter_date_created'];
        }

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['breadcrumbs'] = array();

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('text_home'),
            'href' => $this->url->link('common/home', 'token=' . $this->session->data['token'], 'SSL'),
            'separator' => false
        );

        $this->data['breadcrumbs'][] = array(
            'text' => $this->language->get('heading_title'),
            'href' => $this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . $url, 'SSL'),
            'separator' => ' :: '
        );

        if (!$this->config->get('speedy_from_office')) {
            $this->data['courier'] = $this->url->link('sale/speedy/courier', 'token=' . $this->session->data['token'], 'SSL');
        } else {
            $this->data['courier'] = false;
        }

        $this->data['speedy_orders'] = array();

        $data = array(
            'filter_bol_id' => $filter_bol_id,
            'filter_order_id' => $filter_order_id,
            'filter_customer' => $filter_customer,
            'filter_date_created' => $filter_date_created,
            'sort' => $sort,
            'order' => $order,
            'start' => ($page - 1) * $this->config->get('config_admin_limit'),
            'limit' => $this->config->get('config_admin_limit')
        );

        $speedy_order_total = $this->model_sale_speedy->getTotalOrders($data);

        $results = $this->model_sale_speedy->getOrders($data);

        foreach ($results as $result) {
            $action = array();

            $action[] = array(
                'text' => $this->language->get('text_cancel'),
                'href' => $this->url->link('sale/speedy/cancel', 'token=' . $this->session->data['token'] . '&order_id=' . $result['order_id'] . $url, 'SSL')
            );

            if ($result['courier']) {
                $action[] = array(
                    'text' => $this->language->get('text_courier_sent')
                );
            } elseif (!$this->config->get('speedy_from_office')) {
                $action[] = array(
                    'text' => $this->language->get('text_request_courier'),
                    'href' => $this->url->link('sale/speedy/courier', 'token=' . $this->session->data['token'] . '&order_id=' . $result['order_id'] . $url, 'SSL')
                );
            }

            $action[] = array(
                'text' => $this->language->get('text_track'),
                'href' => 'http://speedy.bg/begin.php?shipmentNumber=' . $result['bol_id'] . '&lang=' . (strtolower($this->config->get('config_admin_language')) == 'bg' ? 'bg' : 'en'),
                'target' => '_blank'
            );

            $this->data['speedy_orders'][] = array(
                'bol_id' => $result['bol_id'],
                'bol_href' => $this->url->link('sale/speedy/printPDF', 'token=' . $this->session->data['token'] . '&order_id=' . $result['order_id'] . $url, 'SSL'),
                'bol_target' => '_blank',
                'order_id' => $result['order_id'],
                'order_href' => $this->url->link('sale/order/info', 'token=' . $this->session->data['token'] . '&order_id=' . $result['order_id'] . $url, 'SSL'),
                'customer' => $result['customer'],
                'date_created' => date($this->language->get('date_format_short') . ' ' . $this->language->get('time_format'), strtotime($result['date_created'])),
                'selected' => isset($this->request->post['selected']) && in_array($result['order_id'], $this->request->post['selected']),
                'action' => $action
            );
        }

        $this->data['heading_title'] = $this->language->get('heading_title');

        $this->data['text_no_results'] = $this->language->get('text_no_results');

        $this->data['column_bol_id'] = $this->language->get('column_bol_id');
        $this->data['column_order_id'] = $this->language->get('column_order_id');
        $this->data['column_customer'] = $this->language->get('column_customer');
        $this->data['column_date_created'] = $this->language->get('column_date_created');
        $this->data['column_action'] = $this->language->get('column_action');

        $this->data['button_filter'] = $this->language->get('button_filter');
        $this->data['button_courier'] = $this->language->get('button_courier');

        $this->data['token'] = $this->session->data['token'];

        if (isset($this->session->data['warning'])) {
            $this->data['error_warning'] = $this->session->data['warning'];

            unset($this->session->data['warning']);
        } elseif (isset($this->error['warning'])) {
            $this->data['error_warning'] = $this->error['warning'];
        } else {
            $this->data['error_warning'] = '';
        }

        if (isset($this->session->data['success'])) {
            $this->data['success'] = $this->session->data['success'];

            unset($this->session->data['success']);
        } else {
            $this->data['success'] = '';
        }

        $url = '';

        if (isset($this->request->get['filter_bol_id'])) {
            $url .= '&filter_bol_id=' . $this->request->get['filter_bol_id'];
        }

        if (isset($this->request->get['filter_order_id'])) {
            $url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
        }

        if (isset($this->request->get['filter_customer'])) {
            $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_date_created'])) {
            $url .= '&filter_date_created=' . $this->request->get['filter_date_created'];
        }

        if ($order == 'ASC') {
            $url .= '&order=DESC';
        } else {
            $url .= '&order=ASC';
        }

        if (isset($this->request->get['page'])) {
            $url .= '&page=' . $this->request->get['page'];
        }

        $this->data['sort_speedy_order'] = $this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . '&sort=so.bol_id' . $url, 'SSL');
        $this->data['sort_order'] = $this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . '&sort=so.order_id' . $url, 'SSL');
        $this->data['sort_customer'] = $this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . '&sort=customer' . $url, 'SSL');
        $this->data['sort_date_created'] = $this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . '&sort=so.date_created' . $url, 'SSL');

        $url = '';

        if (isset($this->request->get['filter_bol_id'])) {
            $url .= '&filter_bol_id=' . $this->request->get['filter_bol_id'];
        }

        if (isset($this->request->get['filter_order_id'])) {
            $url .= '&filter_order_id=' . $this->request->get['filter_order_id'];
        }

        if (isset($this->request->get['filter_customer'])) {
            $url .= '&filter_customer=' . urlencode(html_entity_decode($this->request->get['filter_customer'], ENT_QUOTES, 'UTF-8'));
        }

        if (isset($this->request->get['filter_date_created'])) {
            $url .= '&filter_date_created=' . $this->request->get['filter_date_created'];
        }

        if (isset($this->request->get['sort'])) {
            $url .= '&sort=' . $this->request->get['sort'];
        }

        if (isset($this->request->get['order'])) {
            $url .= '&order=' . $this->request->get['order'];
        }

        $pagination = new Pagination();
        $pagination->total = $speedy_order_total;
        $pagination->page = $page;
        $pagination->limit = $this->config->get('config_admin_limit');
        $pagination->text = $this->language->get('text_pagination');
        $pagination->url = $this->url->link('sale/speedy', 'token=' . $this->session->data['token'] . $url . '&page={page}', 'SSL');

        $this->data['pagination'] = $pagination->render();

        $this->data['filter_bol_id'] = $filter_bol_id;
        $this->data['filter_order_id'] = $filter_order_id;
        $this->data['filter_customer'] = $filter_customer;
        $this->data['filter_date_created'] = $filter_date_created;

        $this->data['sort'] = $sort;
        $this->data['order'] = $order;

        $this->template = 'sale/speedy_list.tpl';
        $this->children = array(
            'common/header',
            'common/footer'
        );

        $this->response->setOutput($this->render());
    }

    public function checkDate() {
        $this->language->load('sale/speedy');

        if (isset($this->request->post['shipping_method_id'])) {
            $shipping_method_id = $this->request->post['shipping_method_id'];
        } else {
            $shipping_method_id = '';
        }

        $json = array();

        $taking_date = ($this->config->get('speedy_taking_date') ? strtotime('+' . (int) $this->config->get('speedy_taking_date') . ' day', mktime(9, 0, 0)) : time());
        $first_available_date = strtotime($this->speedy->getAllowedDaysForTaking(array('shipping_method_id' => $shipping_method_id, 'taking_date' => $taking_date)));

        if (!$this->speedy->getError() && $first_available_date) {
            if (date('d-m-Y', $first_available_date) != date('d-m-Y', $taking_date)) {
                $json['error'] = true;
                $json['warning'] = sprintf($this->language->get('text_confirm_date'), date($this->language->get('date_format_short'), $first_available_date));
                $json['taking_date'] = date('d-m-Y', $first_available_date);
            }
        } else {
            $json['error'] = true;
            $json['warning'] = $this->speedy->getError();
        }

        $this->response->setOutput(json_encode($json));
    }

    protected function validateForm() {
        if (!$this->user->hasPermission('modify', 'sale/speedy')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if ((utf8_strlen($this->request->post['contents']) < 1) || (utf8_strlen($this->request->post['contents']) > 100)) {
            $this->error['contents'] = $this->language->get('error_contents');
        }

        if ($this->request->post['weight'] <= 0) {
            $this->error['weight'] = $this->language->get('error_weight');
        }

        if ((utf8_strlen($this->request->post['packing']) < 1) || (utf8_strlen($this->request->post['packing']) > 50)) {
            $this->error['packing'] = $this->language->get('error_packing');
        }

        if ($this->request->post['count'] <= 0) {
            $this->error['count'] = $this->language->get('error_count');
        }

        if ($this->request->post['postcode'] && $this->request->post['city'] && $this->request->post['city_id'] &&
                (!$this->request->post['to_office'] && (($this->request->post['quarter'] && ($this->request->post['quarter_id'] && $this->request->post['city_nomenclature'] == 'FULL' || $this->request->post['city_nomenclature'] != 'FULL') && ($this->request->post['block_no'] || $this->request->post['street_no'])) ||
                ($this->request->post['street'] && ($this->request->post['street_id'] && $this->request->post['city_nomenclature'] == 'FULL' || $this->request->post['city_nomenclature'] != 'FULL') && ($this->request->post['block_no'] || $this->request->post['street_no'])) || $this->request->post['note']) || ($this->request->post['to_office'] && $this->request->post['office_id']))) {
            
        } else {
            if ($this->request->post['to_office']) {
                $this->error['office'] = $this->language->get('error_office');
            } else {
                $this->error['address'] = $this->language->get('error_address');
            }
        }

        if (isset($this->request->post['fixed_time_cb'])) {
            if (!$this->request->post['fixed_time_hour'] || $this->request->post['fixed_time_hour'] < 10 || $this->request->post['fixed_time_hour'] > 17 ||
                    !$this->request->post['fixed_time_min'] || $this->request->post['fixed_time_min'] < '00' || $this->request->post['fixed_time_min'] > 59 ||
                    ($this->request->post['fixed_time_hour'] == 10 && $this->request->post['fixed_time_min'] < 30) ||
                    ($this->request->post['fixed_time_hour'] == 17 && $this->request->post['fixed_time_min'] > 30)) {
                $this->error['fixed_time'] = $this->language->get('error_fixed_time');
            }
        }

        if ($this->error && !isset($this->error['warning'])) {
            $this->error['warning'] = $this->language->get('error_warning');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

    protected function validateDelete() {
        if (!$this->user->hasPermission('modify', 'sale/speedy')) {
            $this->error['warning'] = $this->language->get('error_permission');
        }

        if (!$this->error) {
            return true;
        } else {
            return false;
        }
    }

    public function getCities() {
        if (isset($this->request->get['term'])) {
            $name = $this->request->get['term'];
        } else {
            $name = '';
        }

        $data = $this->speedy->getCities($name);

        if ($this->speedy->getError()) {
            $data = array('error' => $this->speedy->getError());
        }

        $this->response->setOutput(json_encode($data));
    }

    public function getQuarters() {
        if (isset($this->request->get['term'])) {
            $name = $this->request->get['term'];
        } else {
            $name = '';
        }

        if (isset($this->request->get['city_id'])) {
            $city_id = $this->request->get['city_id'];
        } else {
            $city_id = '';
        }

        if ($city_id) {
            $data = $this->speedy->getQuarters($name, $city_id);

            if ($this->speedy->getError()) {
                $data = array('error' => $this->speedy->getError());
            }
        } else {
            $this->language->load('sale/speedy');
            $data = array('error' => $this->language->get('error_city'));
        }

        $this->response->setOutput(json_encode($data));
    }

    public function getStreets() {
        if (isset($this->request->get['term'])) {
            $name = $this->request->get['term'];
        } else {
            $name = '';
        }

        if (isset($this->request->get['city_id'])) {
            $city_id = $this->request->get['city_id'];
        } else {
            $city_id = '';
        }

        if ($city_id) {
            $data = $this->speedy->getStreets($name, $city_id);

            if ($this->speedy->getError()) {
                $data = array('error' => $this->speedy->getError());
            }
        } else {
            $this->language->load('sale/speedy');
            $data = array('error' => $this->language->get('error_city'));
        }

        $this->response->setOutput(json_encode($data));
    }

    public function getBlocks() {
        if (isset($this->request->get['term'])) {
            $name = $this->request->get['term'];
        } else {
            $name = '';
        }

        if (isset($this->request->get['city_id'])) {
            $city_id = $this->request->get['city_id'];
        } else {
            $city_id = '';
        }

        if ($city_id) {
            $data = $this->speedy->getBlocks($name, $city_id);

            if ($this->speedy->getError()) {
                $data = array('error' => $this->speedy->getError());
            }
        } else {
            $this->language->load('sale/speedy');
            $data = array('error' => $this->language->get('error_city'));
        }

        $this->response->setOutput(json_encode($data));
    }

    public function getOffices() {
        if (isset($this->request->get['term'])) {
            $name = $this->request->get['term'];
        } else {
            $name = '';
        }

        if (isset($this->request->get['city_id'])) {
            $city_id = $this->request->get['city_id'];
        } else {
            $city_id = '';
        }

        if ($city_id) {
            $data = $this->speedy->getOffices($name, $city_id);

            if ($this->speedy->getError()) {
                $data = array('error' => $this->speedy->getError());
            }
        } else {
            $this->language->load('sale/speedy');
            $data = array('error' => $this->language->get('error_city'));
        }

        $this->response->setOutput(json_encode($data));
    }

    public function getQuote() {
        $this->language->load('shipping/speedy');

        $quote_data = array();

        $quote_data['speedy'] = array(
            'code' => 'speedy.speedy',
            'title' => $this->language->get('text_description'),
            'cost' => 0.00,
            'tax_class_id' => 0,
            'text' => $this->currency->format(0.00)
        );

        $method_data = array();

        if ($this->request->server['REQUEST_METHOD'] == 'POST') {
            if ($this->config->get('speedy_documents') && (float) $this->request->post['weight'] > 0.25) {
                $data['weight'] = 0.25;
            }

            $data['taking_date'] = ($this->config->get('speedy_taking_date') ? strtotime('+' . (int) $this->config->get('speedy_taking_date') . ' day', mktime(9, 0, 0)) : time());

            $this->request->post['fixed_time'] = '';

            $methods = $this->speedy->calculate(array_merge($data, $this->request->post));

            if (isset($this->request->post['fixed_time_cb'])) {
                $this->request->post['fixed_time'] = $this->request->post['fixed_time_hour'] . $this->request->post['fixed_time_min'];
            } else {
                $this->request->post['fixed_time'] = '1030';
            }

            if (isset($this->request->post['total'])) {
                $data['total'] = (float) $this->request->post['total'];
            }

            $methods_fixed_time = $this->speedy->calculate(array_merge($data, $this->request->post));

            $this->session->data['loading_methods_fixed_time'] = array();

            foreach ($methods_fixed_time as $method) {
                if (!$method->getErrorDescription()) {
                    $this->session->data['loading_methods_fixed_time'][$method->getServiceTypeId()] = $method->getResultInfo()->getAmounts()->getTotal();
                }
            }

            $services = $this->speedy->getServices();
            $methods_count = 0;

            if (!$this->speedy->getError()) {
                foreach ($methods as $method) {
                    if (!$method->getErrorDescription()) {
                        if (($this->config->get('speedy_pricing') == 'free') && ($data['total'] >= (float) $this->config->get('speedy_free_shipping_total')) &&
                                ($method->getServiceTypeId() == $this->config->get('speedy_free_method_city') || $method->getServiceTypeId() == $this->config->get('speedy_free_method_intercity'))) {
                            $method_total = 0;
                        } elseif ($this->config->get('speedy_pricing') == 'fixed') {
                            $method_total = $this->config->get('speedy_fixed_price');
                        } else {
                            if (isset($this->request->post['fixed_time_cb']) && isset($this->session->data['loading_methods_fixed_time'][$method->getServiceTypeId()])) {
                                $method_total = $this->session->data['loading_methods_fixed_time'][$method->getServiceTypeId()];
                            } else {
                                $method_total = $method->getResultInfo()->getAmounts()->getTotal();
                            }

                            if ($this->config->get('speedy_pricing') == 'calculator_fixed') {
                                $method_total += $this->config->get('speedy_fixed_price');
                            }
                        }
                        
                        $this->session->data['shipping_method_cost'][$method->getServiceTypeId()] = $method_total;
                        $this->session->data['shipping_method_title'][$method->getServiceTypeId()] = $services[$method->getServiceTypeId()];

                        $quote_data[$method->getServiceTypeId()] = array(
                            'code' => 'speedy.' . $method->getServiceTypeId(),
                            'title' => $this->language->get('text_description') . ' - ' . $services[$method->getServiceTypeId()],
                            'cost' => $method_total,
                            'tax_class_id' => 0,
                            'text' => $this->currency->format($method_total, $this->config->get('speedy_currency'), 1)
                        );

                        $methods_count++;
                    }
                }

                if ($methods_count) {
                    unset($quote_data['speedy']);
                    $method_data['quote'] = $quote_data;
                }
            } else {
                $method_data['speedy_error'] = $this->speedy->getError();
            }
        } else {
            $method_data['speedy_error'] = $this->language->get('error_calculate');
        }

        if (isset($method_data['speedy_error'])) {
            $method_data['quote']['speedy']['text'] = '';
        }

        return $method_data;
    }

}

?>